package mypackage.dao.impl;

import mypackage.bean.GoodOrder;
import mypackage.bean.Goods;
import mypackage.dao.ClientsDao;
import mypackage.dao.GoodOrderDao;
import mypackage.dao.GoodsDao;
import mypackage.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author YH925
 * @create 2021/12/10 22:02
 */
public class GoodOrderDaoImpl implements GoodOrderDao {

    Connection connection;

    {
        try {
            connection = JDBCUtils.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    QueryRunner runner = new QueryRunner();


    /**
     * 将商品加入购物车
     * @param clientId
     * @param goodId
     * @param amount
     * @param price
     */
    @Override
    public void insertData(int clientId, String goodId, int amount, double price) {
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql = "insert into GoodOrders values(?,?,?,?,to_date(?,'YYYY-MM-DD HH24:MI:SS'),?)";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm;ss");
        String time = dateFormat.format(date);

        try {
            runner.update(connection,sql,clientId,goodId,amount,price,time,null);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }

    }

    @Override
    public List<GoodOrder> findGoodChart(int clientId) {
        String sql = "select * from GoodOrders where clientId = ?";
        BeanListHandler<GoodOrder>handler = new BeanListHandler<>(GoodOrder.class);
        List<GoodOrder> orderList = null;
        try {
            orderList = runner.query(connection, sql, handler, clientId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orderList;
    }

    @Override
    public List<GoodOrder> findGoodChartWithGood(int clientId) {
        List<GoodOrder> goodChart = findGoodChart(clientId);
        GoodsDao dao = new GoodsImpl();
        for (GoodOrder goodOrder : goodChart) {
            String goodId = goodOrder.getGoodId();
            Goods goods = dao.selectGoodsById(goodId);
            goodOrder.setGoods(goods);
        }

        return goodChart;
    }

    @Override
    public int findTotalGoodsFromChart(int clientId) {
        String sql = "select count(*) from GoodOrders where clientId = ?";
        ScalarHandler<Object>handler = new ScalarHandler<>();
        Object obj = null;
        try {
            obj = runner.query(connection, sql, handler, clientId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Integer count = Integer.parseInt(obj.toString());
        return count;
    }

    @Override
    public double findTotalPriceFromChart(int clientId) {
        String sql = "select sum(price) from GOODORDERS where CLIENTID = ?";
        ScalarHandler<Object>handler = new ScalarHandler<>();
        Object obj = null;
        try {
            obj = runner.query(connection, sql, handler, clientId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Double price = Double.parseDouble(obj.toString());
        return price;
    }

    @Override
    public void deleteAllGoodsByClientId(int clientId) {
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        String sql = "delete from GOODORDERS where CLIENTID = ?";
        try {
            runner.update(connection, sql, clientId);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    @Override
    public void deleteOneGoodById(int id) {
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql = "delete from GOODORDERS where id = ?";
        try {
            runner.update(connection,sql,id);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    @Override
    public GoodOrder selectGoodFromChartById(int id) {
        String sql = "select * from goodOrders where id = ?";
        BeanHandler<GoodOrder> handler = new BeanHandler<>(GoodOrder.class);
        GoodOrder order = null;
        try {
            order = runner.query(connection, sql, handler, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return order;
    }

    /**
     * 修改购物车中商品的数量  设置事务√
     * @param id
     * @param newAmount
     */
    @Override
    public void updateGoodAmount(int id, int newAmount) {
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        GoodOrder order = selectGoodFromChartById(id);
        String sql = "update goodOrders set amount = ?,price = ? where clientId = ? and goodId = ?";

        String goodId = order.getGoodId();
        int clientId = order.getClientId();

        GoodsImpl goods = new GoodsImpl();
        Goods goodsById = goods.selectGoodsById(goodId);
        double price = goodsById.getOutputMoney() * newAmount;

        try {
            runner.update(connection,sql,newAmount,price,clientId,goodId);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 查询用户是否购买过当前浏览的商品
     * @param clientId
     * @param goodId
     * @return
     */
    @Override
    public GoodOrder isExistCurrentGood(int clientId, String goodId) {
        String sql = "select * from goodOrders where clientId = ? and goodId = ?";
        BeanHandler<GoodOrder> handler = new BeanHandler<>(GoodOrder.class);
        GoodOrder goodOrder = null;
        try {
            goodOrder = runner.query(connection, sql, handler, clientId, goodId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodOrder;
    }
}
